﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Service.Redis
{
    public class RedisService: IRedisService
    {
        private readonly IDatabase _database;
        private readonly bool IsEnable;

        public RedisService(IConnectionMultiplexer redis)
        {
            IsEnable = redis != null;
            _database = IsEnable ? redis.GetDatabase() : null;
        }

        public bool Set(string key, string value, TimeSpan? expiry = null)
        {
            if (!IsEnable) return false; // Redis 未启用，直接返回

            return _database.StringSet(key, value, expiry);
        }

        public string? Get(string key)
        {
            if (!IsEnable) return null; // Redis 未启用，返回空值

            return _database.StringGet(key);
        }

        public bool Delete(string key)
        {
            if (!IsEnable) return false; // Redis 未启用，直接返回

            return _database.KeyDelete(key);
        }

        public long Increment(string key, long value = 1)
        {
            if (!IsEnable) return 0; // Redis 未启用，直接返回

            return _database.StringIncrement(key, value);
        }

        public long Decrement(string key, long value = 1)
        {
            if (!IsEnable) return 0; // Redis 未启用，直接返回

            return _database.StringDecrement(key, value);
        }
    }
}
